package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.decoration.UserMammonDecoration;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMammonDecorationDaoImpl extends TempDataObjectDao<UserMammonDecoration> {

    public static String getUserMammonDecorationSql = "select * from `t_u_mammon_decoration` where `userId`=?";
    public Map<Integer,UserMammonDecoration> getUserMammonDecoration(long userId) {
        Map<Integer,UserMammonDecoration> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserMammonDecorationSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserMammonDecoration userMammonDecoration = parse(rs);
                map.put(userMammonDecoration.getMammonType(),userMammonDecoration);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateUserMammonDecorationSql = "update `t_u_mammon_decoration` set num=? where userId=? and mammonType=?";
    @Override
    protected boolean updateData(UserMammonDecoration userMammonDecoration) {
        userMammonDecoration.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userMammonDecoration.getNum()));
        para.put(i++, new DbParameter(Types.BIGINT, userMammonDecoration.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMammonDecoration.getMammonType()));
        if (getDb().executeNoneQuery(updateUserMammonDecorationSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserMammonDecorationSql = "insert into `t_u_mammon_decoration`(userId,mammonType,num) values (?,?,?)";
    @Override
    protected boolean addData(UserMammonDecoration userMammonDecoration) {
        userMammonDecoration.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userMammonDecoration.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userMammonDecoration.getMammonType()));
        para.put(i++, new DbParameter(Types.INTEGER, userMammonDecoration.getNum()));
        if (getDb().executeNoneQuery(insertUserMammonDecorationSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserMammonDecoration parseData(ResultSet rs) throws SQLException {
        UserMammonDecoration userMammonDecoration = new UserMammonDecoration();
        userMammonDecoration.setUserId(rs.getLong("userId"));
        userMammonDecoration.setMammonType(rs.getInt("mammonType"));
        userMammonDecoration.setNum(rs.getInt("num"));

        return userMammonDecoration;
    }

}
